#include <stdlib.h>
#include "device.h"
#include "widget.h"

extern UIState uistate;
extern unsigned int* mask_fb;
extern unsigned int* menu_fb[3];

static int SliderInt[32];
static int btnval[32];
static int cdzt[8] = {63,64,4096,4097,4098,4099}; //������ʾ���� ,  ��ͼ�ꡱ�˵� -0
static int ckjl[8] = {4206,4208,4132,4134}; //���˵����쿴��¼
static int fdcs[8] = {4268,4116,4106,4272}; //���˵����ŵ����
static int cxxz[8] = {4124,4126,4112,4114}; //���˵�������ѡ��
static int cxxg[8] = {4124,4126,4112,4114}; //���˵��������޸�
static int cssz[8] = {4260,4262,4258,4216}; //���˵�����������
static int sjsz[8] = {4182,4184,4260,4262}; //���˵���ʱ������
static int smcx[8] = {4098,4158};           //��ģ����
static int mmxc[8] = {4160,4158};           //��ģ����
static int sswy[8] = {4162,4164,4150,4166}; //ɫɢλ�Ƴ���
static int nzwy[8] = {4168,4170,4162,4164}; //����ɫɢ����
static int cecx[8] = {4172,4174};           //�������
static int yrsj[8] = {4180,4128,4182,4184}; //Ԥ��ʱ��
static int yrqd[8] = {4180,4128,4186,4188}; //Ԥ��ǿ��
static int rjsj[8] = {4128,4130,4182,4184}; //�۽�ʱ��
static int rjqd[8] = {4128,4130,4186,4188}; //�۽�ǿ��
static int tjlg[8] = {4190,4192,4142,4194}; //�ƽ���
static int tjsd[8] = {4190,4192,4196,4188}; //�ƽ��ٶ�
static int dmjd[8] = {4198,4200,4202,4188}; //����Ƕ�
static int zlcs[8] = {4386,4388,4116,4118}; //��������
static int djsd[8] = {4272,4320,4196,4188}; //����ٶ�
static int tjzt[8] = {4190,4192,4286,4288}; //�ƽ���ͣ
static int dxzt[8] = {4366,4368,4286,4288}; //��о��ͣ
static int gzfs[8] = {4290,4292,4294,4296}; //������ʽ
static int jrsj[8] = {4298,4300,4182,4184}; //����ʱ��
static int jxwz[8] = {4106,4272,4150,4262}; //��϶λ��
static int zdrj[8] = {4100,4102,4128,4130}; //�Զ��۽�
static int zdjr[8] = {4100,4102,4298,4300}; //�Զ�����
static int nian[8] = {4330}; //��
static int yue_[8] = {4334}; //��
static int ri__[8] = {4224}; //��
static int shi_[8] = {4182}; //ʱ
static int fen_[8] = {4284}; //��
static int fdjz[8] = {4106,4272,4520,4326,0,0,0,0}; 
static int rjjl[8] = {4128,4130,4132,4134,0,0,0,0};
static int gzms[8] = {0,0,4290,4292,4158,4296,0,0};
static int xtsz[8] = {4264,4266,4260,4262,0,0,0,0}; 
static int qdbn[8] = {4144,4146};

void page_0(){
    if(button(GEN_ID,20,20 + MAX_Y/10,(MAX_X-60)/3, MAX_Y/4,COLOR_ORIG,xtsz,1)) {
      uistate.show_page = 1;
    }
    if(button(GEN_ID,32+(MAX_X-60)/3,20 + MAX_Y/10,(MAX_X-60)/3, MAX_Y/4,COLOR_CYAN,rjjl,1)) {
      uistate.show_page = 2;
    }
    if(button(GEN_ID,44+2*(MAX_X-60)/3,20 + MAX_Y/10,(MAX_X-60)/3, MAX_Y/4,COLOR_GREEN,fdjz,1)) {
      uistate.show_page = 3;
    }
    if (button(GEN_ID,20, 30 + MAX_Y/6 + MAX_Y/4,2*(MAX_X-60)/3+12, MAX_Y/4,COLOR_PURPLE,gzms,1)) {
      uistate.show_page = 4;
    }
    if (button(GEN_ID,44+2*(MAX_X-60)/3 ,30 + MAX_Y/6 + MAX_Y/4,(MAX_X-60)/3, MAX_Y/4,COLOR_YELLOW,sjsz,1)) {
      uistate.show_page = 5;
    }
}

void page_1(){
    int  slidervalue;
    if(uistate.keyentered == SDLK_ESCAPE) {uistate.show_page = 0;}
    //int lbtxt[8] = {0,0,0,4268,4116,4106,4272,0}; 
    disp_str16(10  ,14 + 36*0 + 6,UI_BG,dmjd);
    disp_str16(10  ,14 + 36*1 + 6,UI_BG,jrsj);
    disp_str16(10  ,14 + 36*2 + 6,UI_BG,jxwz);
    disp_str16(10  ,14 + 36*3 + 6,UI_BG,rjqd);
    disp_str16(10  ,14 + 36*4 + 6,UI_BG,rjsj);
    disp_str16(250 ,14 + 36*0 + 6,UI_BG,tjlg);
    disp_str16(250 ,14 + 36*1 + 6,UI_BG,tjsd);
    disp_str16(250 ,14 + 36*2 + 6,UI_BG,djsd);
    disp_str16(250 ,14 + 36*3 + 6,UI_BG,yrqd);
    disp_str16(250 ,14 + 36*4 + 6,UI_BG,yrsj);
    //disp_str16(20  ,240 +26,UI_BG,zdrj);
    //disp_str16(20  ,280 +26,UI_BG,zdjr);
    //disp_str16(20  ,320 +26,UI_BG,zlcs);
    //disp_str16(320 ,240 +26,UI_BG,tjzt);
    //disp_str16(320 ,280 +26,UI_BG,dxzt);

    slidervalue = SliderInt[0];
    if (slider(GEN_ID,80, 14 + 36*0, &slidervalue, 255)) {
        SliderInt[0] = slidervalue;
    }
    
    slidervalue = SliderInt[1];
    if (slider(GEN_ID, 80,14 + 36*1, &slidervalue, 63)) {
       SliderInt[1] = slidervalue;
    }
    
    slidervalue = SliderInt[2];
    if (slider(GEN_ID, 80, 14 + 36*2, &slidervalue, 15)) {
       SliderInt[2] = slidervalue;
    }
    
    slidervalue = SliderInt[3];
    if (slider(GEN_ID, 80, 14 + 36*3, &slidervalue, 15)) {
       SliderInt[3] = slidervalue;
    }
    
    slidervalue = SliderInt[7];
    if (slider(GEN_ID, 80, 14 + 36*4, &slidervalue, 15)) {
       SliderInt[7] = slidervalue;
    }

    slidervalue = SliderInt[4];
    if (slider(GEN_ID,320, 14 + 36*0, &slidervalue, 255)) {
        SliderInt[4] = slidervalue;
    }
    
    slidervalue = SliderInt[5];
    if (slider(GEN_ID, 320, 14 + 36*1, &slidervalue, 63)) {
       SliderInt[5] = slidervalue;
    }
    
    slidervalue = SliderInt[6];
    if (slider(GEN_ID, 320, 14 + 36*2, &slidervalue, 15)) {
       SliderInt[6] = slidervalue;
    }

    slidervalue = SliderInt[8];
    if (slider(GEN_ID, 320, 14 + 36*3, &slidervalue, 15)) {
       SliderInt[8] = slidervalue;
    }

    slidervalue = SliderInt[9];
    if (slider(GEN_ID, 320, 14 + 36*4, &slidervalue, 15)) {
       SliderInt[9] = slidervalue;
    }

    //tactvalue = SliderInt[10];
    //if (tactswitch(GEN_ID, 90, 243 +20,tactvalue)) {
    //   SliderInt[10] = tactvalue;
    //}
 
    //tactvalue = SliderInt[11];
    //if (tactswitch(GEN_ID, 90, 283 +20,tactvalue)) {
    //   SliderInt[11] = tactvalue;
    //}

    //tactvalue = SliderInt[12];
    //if (tactswitch(GEN_ID, 90, 323 +20,tactvalue)) {
    //   SliderInt[12] = tactvalue;
    //}  

    //tactvalue = SliderInt[13];
    //if (tactswitch(GEN_ID,390, 243 +20,tactvalue)) {
    //   SliderInt[13] = tactvalue;
    //}    

    //tactvalue = SliderInt[14];
    //if (tactswitch(GEN_ID,390, 283 +20,tactvalue)) {
    //   SliderInt[14] = tactvalue;
    //}    

    if(button(GEN_ID,(MAX_X-105),20 + (MAX_Y - 74),60,28,COLOR_LGRAY,qdbn,0)) {
        uistate.show_page = 0;
        uistate.kbditem = 0;
    }
}

void page_2(){
    int  slidervalue;
    if(uistate.keyentered == SDLK_ESCAPE) {uistate.show_page = 0;}
    slidervalue = SliderInt[4];
    if (slider(GEN_ID, 40, 100, &slidervalue, 255)) {
       SliderInt[4] = slidervalue;
    }
    
    slidervalue = SliderInt[5];
    if (slider(GEN_ID, 40, 150, &slidervalue, 15)) {
       SliderInt[5] = slidervalue;
    }
    
    if(button(GEN_ID,(MAX_X-105),(MAX_Y - 74),60,28,COLOR_LGRAY,qdbn,0)) {
        uistate.show_page = 0;
        uistate.kbditem = 0;
    }
}

void page_3(){
	int nI,nJ;
    if(uistate.keyentered == SDLK_ESCAPE) {uistate.show_page = 0;}
    for(nI =0;nI < 12;nI ++){
        for(nJ =0;nJ < 24;nJ ++){
            disp_common16(20 + nJ*18,20 + nI*18,4096 + (24*nI + nJ)*2,UI_BG);
        }
    }
}

void page_4(){
    if(uistate.keyentered == SDLK_ESCAPE) {uistate.show_page = 0;}

    disp_rectangle(0,MAX_Y/2-1,16,MAX_Y/2+1,COLOR_WHITE);
    disp_rectangle(64,MAX_Y/2-1,198,MAX_Y/2+1,COLOR_WHITE);
    disp_rectangle(300,MAX_Y/2-1,MAX_X,MAX_Y/2+1,COLOR_WHITE);

    int lb1[2][8] = {{4100,4102},{4310,4102}}; // {auto , mannul}
    label(GEN_ID,16  ,122, 42, 28,UI_BG,lb1[0]);
    int lb2[2][8] = {{4228,4230,1,86,1,3,40,38},{4128,4130,4398,4400,86,86,86}}; // {loss , db}
    label(GEN_ID,200  ,122, 96, 28,UI_BG,lb2[0]);

    drawBattery(430,16,2);
    drawTime (390,MAX_Y-26,COLOR_TIME);
    drawHeat (220,MAX_Y-28,1);

    sclbtn(GEN_ID,16 ,12, 24, 24,COLOR_GRAY);
    sclbtn(GEN_ID,16 ,MAX_Y-36 , 24, 24,COLOR_GRAY );
}

void page_5() {
    if(uistate.keyentered == SDLK_ESCAPE) {uistate.show_page = 0;}
    int btnvalue = 0;
    int tmset[8] = {3,1,2,10,0,0,0,0};
    if(valbtn(GEN_ID,60,60,80, 38,COLOR_DGRAY,tmset,&btnvalue)) {
        btnval[0] = btnvalue;
        btnvalue = 0;
        //adjust time set year
    }   
    disp_common_big(146,62,4330,UI_BG);
    tmset[0] = 1;
    tmset[1] = 9;
    tmset[2] = 0;
    tmset[3] = 0;
    if(valbtn(GEN_ID,180,60,50, 38,COLOR_DGRAY,tmset,&btnvalue)) {
        btnval[0] = btnvalue;
        btnvalue = 0;
        //adjust time set month
    } 
    disp_common_big(236,62,4334,UI_BG);
    if(valbtn(GEN_ID,270,60,50, 38,COLOR_DGRAY,tmset,&btnvalue)) {
        btnval[0] = btnvalue;
        btnvalue = 0;
        //adjust time set second
    }            
    disp_common_big(326,62,4224,UI_BG);
    if(button(GEN_ID,(MAX_X-105),(MAX_Y - 74),60,28,COLOR_LGRAY,qdbn,0)) {
        uistate.show_page = 0;
        uistate.kbditem = 0;
    }
}

void show_menu_page() {
    switch (uistate.show_page) {
        case 0:
            page_0(); break;
        case 1:
            page_1(); break;
        case 2:
            page_2(); break;
        case 3:
            page_3(); break;
        case 4:
            page_4(); break;
        case 5:
            page_5(); break;
        default:
            page_0();
    }           
}

void imgui_prepare()
{
    uistate.hotitem = 0;
    if(uistate.redraw == 1) disp_rectangle(0,0,MAX_X,MAX_Y,UI_BG); // clear screen
}

void imgui_finish()
{
    if (uistate.mousedown == 0) {
        uistate.activeitem = 0;
    }
    else if (uistate.activeitem == 0) {
        uistate.activeitem = -1;
    }
    // If no widget grabbed tab, clear focus
    if ((uistate.keyentered == SDLK_UP)||(uistate.keyentered == SDLK_DOWN)) {
        uistate.kbditem = 0;
    }
    // Clear the entered key
    uistate.keyentered = 0;  
    uistate.keypressed = 0;  
}

void render()
{   
	if(uistate.keypressed == 1){
		uistate.redraw = 2;
		uistate.next_fb ++;
	}
    if(uistate.redraw == 1) mask_fb = menu_fb[uistate.next_fb&0x1];
    if(uistate.redraw == 0) return;
    printf("uistate.next_fb=%d redraw = %d\n",uistate.next_fb,uistate.redraw);
    imgui_prepare();
    show_menu_page();
    imgui_finish(); 
    uistate.redraw --;
}
 
